<?php

class Admin_WebconfigController extends Zend_Controller_Action
{

    private $_model = null;

    public function init()
    {
        /* Initialize action controller here */
        
    }

    public function indexAction()
    {
        // action body
        
    }
    
    public function developerAction()
    {
        if(APPLICATION_ENV!="development")
        {
            return $this->_redirect($this->view->url(array(
                "module"=>"admin",
                "controller"=>"index",
                "action"=>"index"
            )));
        }
    }
    
    public function addCtrlAction()
    {
        $idmodule = $this->_request->getParam("idmd",null);
        if($idmodule!=null)
        {
            $form = new Application_Form_Controller();
            if($this->_request->isPost())
            {
                if($form->isValid($_POST))
                {
                    $mdCtrl = new Application_Model_DbTable_Controller();
                    $row = $mdCtrl->createRow($form->getValues());
                    $row->modules_id = $idmodule;
                    $row->save();
                    
                    $this->_redirect($this->view->url(array(
                        "module"=>"admin",
                        "controller"=>"webconfig",
                        "action"=>"developer"
                    )));
                    
                }
                else
                {
                    $errors = $form->getMessages();
                    foreach($errors as $key=>$value){
                        $form->$key->setAttrib("class","error");
                    }
                }
            }
            $this->view->form = $form;
        }
    }
    
    
    
    public function delCtrlAction()
    {
        $idcontroller = $this->_request->getParam("id",null);
        if($idcontroller != null)
        {
            $mdCtrl = new Application_Model_DbTable_Controller();
            $row = $mdCtrl->find($idcontroller)->current();
            $row->delete();
             $this->_redirect($this->view->url(array(
                "module"=>"admin",
                "controller"=>"webconfig",
                "action"=>"developer"
            ), null, true));
        }
    }
    
    public function editCtrlAction()
    {
        $idctrl = $this->_request->getParam("id",null);
        if($idctrl!=null)
        {
            $mdCtrl = new Application_Model_DbTable_Controller();
            $ctrl = $mdCtrl->find($idctrl)->current();
            $form = new Application_Form_Controller();
            $form->removeElement("idcontrollers");
            $form->submit->setLabel("Luu");
            $data = $ctrl->toArray();
            $form->populate($data);
            
            if($this->_request->isPost())
            {
                if($form->isValid($_POST))
                {
                    $form_data = $form->getValues();
                    $ctrl->controller_name = $form_data['controller_name'];
                    $ctrl->display_name = $form_data['display_name'];
                    $ctrl->save();
                     $this->_redirect($this->view->url(array(
                        "module"=>"admin",
                        "controller"=>"webconfig",
                        "action"=>"developer"
                    )));
                }
            }
            
            $this->view->form = $form;
        }
    }
    
    
    public function addActAction()
    {
        $idcontroller = $this->_request->getParam("idctrl",null);
        if($idcontroller!=null)
        {
            $form = new Application_Form_Action();
            if($this->_request->isPost())
            {
                if($form->isValid($_POST))
                {
                    $mdAction = new Application_Model_DbTable_Action();
                    $row = $mdAction->createRow($form->getValues());
                    $row->controllers_id = $idcontroller;
                    $idaction = $row->save();
                    
                    $mdGroup = new Application_Model_DbTable_UserGroup();
                    $mdRule = new Application_Model_DbTable_Rule();
                    $groups = $mdGroup->fetchAll();
                    foreach($groups as $group)
                    {
                        $rule = $mdRule->createRow(array(
                            "groups_id"=>$group->idgroups,
                            "actions_id"=>$idaction,
                            "status"=>1
                        ));
                        $rule->save();
                    }                    
                
                    $this->_redirect($this->view->url(array(
                        "module"=>"admin",
                        "controller"=>"webconfig",
                        "action"=>"developer"
                    )));       
                }
                else
                {
                    $errors = $form->getMessages();
                    foreach($errors as $key=>$value){
                        $form->$key->setAttrib("class","error");
                    }
                }
            }
            $this->view->form = $form;
        }
    }
    
    public function delActAction()
    {
        $idact = $this->_request->getParam("id",null);
        if($idact != null)
        {
            $mdAct = new Application_Model_DbTable_Action();
            $row = $mdAct->find($idact)->current();
            $row->delete();
             $this->_redirect($this->view->url(array(
                "module"=>"admin",
                "controller"=>"webconfig",
                "action"=>"developer"
            ), null, true));
        }
    }
    
    public function editActAction()
    {
        $idact= $this->_request->getParam("id",null);
        if($idact!=null)
        {
            $mdAct = new Application_Model_DbTable_Action();
            $act = $mdAct->find($idact)->current();
            $form = new Application_Form_Action();
            $form->removeElement("idactions");
            $form->submit->setLabel("Luu");
            $data = $act->toArray();
            $form->populate($data);
            
            if($this->_request->isPost())
            {
                if($form->isValid($_POST))
                {
                    $form_data = $form->getValues();
                    $act->action_name = $form_data['action_name'];
                    $act->display_name = $form_data['display_name'];
                    $act->save();
                     $this->_redirect($this->view->url(array(
                        "module"=>"admin",
                        "controller"=>"webconfig",
                        "action"=>"developer"
                    )));
                }
            }
            
            $this->view->form = $form;
        }
    }
}













